package Screen
{
	import Button.UglyButton;
	import Button.UglyTextField;
	
	import Const.ButtonIDs;
	
	import InfoGroup.StationInputData;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextFieldType;
	
	public final class StationScreen extends GenericScreen
	{
		private var _stationVector : Vector.<StationInputData> = new Vector.<StationInputData>;
		private var _sectorVMaxVector : Vector.<UglyTextField> = new Vector.<UglyTextField>;
		
		private var _newStationButton : UglyButton;
		private var _saveButton : UglyButton;
		private var _removeStationButton : UglyButton;
		
		public function StationScreen()
		{
			super();
			
			//Add station Button
			_newStationButton = new UglyButton("+ Estacion", ButtonIDs.BUTTONID_ADDSTATION);
			_newStationButton.x = 10;
			_newStationButton.y = 20;
			
			addChild(_newStationButton);
			
			//Remove station Button
			_removeStationButton = new UglyButton("- Estacion", ButtonIDs.BUTTONID_REMOVESTATION);
			_removeStationButton.x = 140;
			_removeStationButton.y = 20;
			
			addChild(_removeStationButton);
			
			//Save button
			_saveButton = new UglyButton("Guardar", ButtonIDs.BUTTONID_SAVE);
			_saveButton.x = 660;
			_saveButton.y = 20;
			
			addChild(_saveButton);
			
			//Add the first station
			addStationAtPos(10, 60);
			//Add another station + vMax sector
			newStationWithoutMouseEvent();			
			
			//Add all the listeners
			addListener();
		}
		
		public function getStationsVector() : Vector.<StationInputData>
		{
			return _stationVector;
		}
		
		public function getSectorVMaxVector() : Vector.<UglyTextField>
		{
			return _sectorVMaxVector;
		}
		
		public function addStationAtPos(posX:Number, posY:Number) : void
		{
			var stationNumer : int = _stationVector.length + 1;
			var tmpStation : StationInputData = new StationInputData(stationNumer);
			tmpStation.x = posX;
			tmpStation.y = posY;
			_stationVector.push(tmpStation);
			
			addChild(tmpStation);
		}
		
		private function addListener() : void
		{
			_newStationButton.addEventListener(MouseEvent.CLICK, newStation);
			_saveButton.addEventListener(MouseEvent.CLICK, saveInput);
			_removeStationButton.addEventListener(MouseEvent.CLICK, removeStation);
		}
		
		private function newStation(evt:MouseEvent) : void
		{
			var Y_OFFSET : int = 60;
			
			var tmpStation : StationInputData = _stationVector[_stationVector.length - 1];
			addStationAtPos(tmpStation.x, tmpStation.y + Y_OFFSET);
			
			var tmpPosX : Number = _stationVector[_stationVector.length - 2].x + 500; //-2 por el anteultimo
			var tmpPosY : Number = _stationVector[_stationVector.length - 2].y + (_stationVector[_stationVector.length - 1].y - _stationVector[_stationVector.length - 2].y);
				
			var tmpTextField : UglyTextField = new UglyTextField(TextFieldType.INPUT, tmpPosX, tmpPosY);
			tmpTextField.setText("0");
			tmpTextField.addLabel("V.Max Sector", "Top");
			tmpTextField.addLabel("Km/h", "Right");
			
			_sectorVMaxVector.push(tmpTextField);			
			addChild(tmpTextField);
		}
		
		private function removeStation(evt:MouseEvent) : void
		{
			var vLenght : int = _stationVector.length;
			
			if(vLenght > 2)
			{
				removeChild(_stationVector[vLenght - 1]);
				_stationVector.splice(vLenght - 1, 1);				
			}
			
			var vLenght2 : int = _sectorVMaxVector.length;
			if(vLenght2 > 1)
			{
				removeChild(_sectorVMaxVector[vLenght2 - 1]);
				_sectorVMaxVector.splice(vLenght2 - 1, 1);
			}
		}
		
		private function newStationWithoutMouseEvent() : void
		{
			var Y_OFFSET : int = 60;
			
			var tmpStation : StationInputData = _stationVector[_stationVector.length - 1];
			addStationAtPos(tmpStation.x, tmpStation.y + Y_OFFSET);
			
			var tmpPosX : Number = _stationVector[_stationVector.length - 2].x + 500; //-2 por el anteultimo
			var tmpPosY : Number = _stationVector[_stationVector.length - 2].y + (_stationVector[_stationVector.length - 1].y - _stationVector[_stationVector.length - 2].y);
			
			var tmpTextField : UglyTextField = new UglyTextField(TextFieldType.INPUT, tmpPosX, tmpPosY);
			tmpTextField.setText("0");
			tmpTextField.addLabel("V.Max Sector", "Top");
			tmpTextField.addLabel("Km/h", "Right");
			tmpTextField.allowSymbol(".");
			
			_sectorVMaxVector.push(tmpTextField);			
			addChild(tmpTextField);
		}
		
		private function saveInput(evt:MouseEvent) : void
		{
			trace("GUARDANDO DATOS DE LAS ESTACIONES");
			
			//First clear the list			
			DistanceObjCreator.clearStationList();

			
			for(var i:int = 0; i < _stationVector.length; i++)
			{				
				if((i+1) < _stationVector.length)
				{
					var tmpTime : Number = (_stationVector[i+1].tiempoDeten.getTextAsNumber() < 0 ? _stationVector[i+1].tiempoDeten.getTextAsNumber()*(-1) : _stationVector[i+1].tiempoDeten.getTextAsNumber()); //The time can not be negative					
					//Creates the sectors
					//divido por 1000 la distancia para manejar todo en metros
					//divido  por 3.6 la vmax para pasarlo a m/s 
					DistanceObjCreator.createDistObj(_stationVector[i].distancia.getTextAsNumber() * 1000, 
						_stationVector[i+1].distancia.getTextAsNumber() * 1000, tmpTime, _sectorVMaxVector[i].getTextAsNumber() / 3.6); //convierto a m/s
				}
			}			
							
			//For Debug SECTORS
			var stationList:Array = DistanceObjCreator.stationsList;
			for (var j:int = 0; j < stationList.length; j++)
			{
				trace( "Sector " + j + " Init Pos : " + stationList[j].initPos );
				trace( "Sector " + j + " Dest Pos : " + stationList[j].destPos );
				trace( "Sector " + j + " MaxVelocity : " + stationList[j].segmentVMax );
				
				trace("=========================================================");
			}
		}
	}
}